package com.deyuanyun.pic.ae.service.impl;

import com.deyuanyun.pic.ae.controller.dto.AEPipeLineQuery;
import com.deyuanyun.pic.ae.domain.AEPipeline;
import com.deyuanyun.pic.ae.domain.TrafficFacilities;
import com.deyuanyun.pic.ae.domain.TrafficFacilitiesExample;
import com.deyuanyun.pic.ae.enumeration.DataStatedEnum;
import com.deyuanyun.pic.ae.enumeration.IsNewDateEnum;
import com.deyuanyun.pic.ae.enumeration.PositionRelationEnum;
import com.deyuanyun.pic.ae.mapper.TrafficFacilitiesMapper;
import com.deyuanyun.pic.ae.mapper.dao.TrafficFacilitiesDao;
import com.deyuanyun.pic.ae.service.AEBaseService;
import com.deyuanyun.pic.ae.service.TrafficFacilitiesService;
import com.deyuanyun.pic.common.util.CommonUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.settings.support.biz.AttachmentUtil;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * “交通设施” 服务接口 实现类
 *
 * @author huanglw
 * @date  2016-11-11
 *
 * History: [修改人][时间][修改描述]
 */
@Service
public class TrafficFacilitiesServiceImpl extends AEBaseService implements TrafficFacilitiesService {

    @Resource
    private TrafficFacilitiesMapper trafficFacilitiesMapper;
    @Resource
    private TrafficFacilitiesDao trafficFacilitiesDao;

    @Override
    public Page<TrafficFacilities> query(AEPipeLineQuery aePipeLineQuery) {
        return trafficFacilitiesDao.query(aePipeLineQuery);
    }

    @Override
    public TrafficFacilities find(String id) {
        return trafficFacilitiesMapper.selectByPrimaryKey(id);
    }

    @Override
    public int edit(TrafficFacilities trafficFacilities, List<String> imgs) {

        if (PositionRelationEnum.CROSS.toString().equals(trafficFacilities.getRelativePositionRelation())) {
            // 计算交叉绝对里程
            PositionObj crossPositionObj = getPositionInfo(trafficFacilities.getPipeLineId(), trafficFacilities.getCrossX(), trafficFacilities.getCrossY());
            if (crossPositionObj != null) {
                trafficFacilities.setCrossMileage(crossPositionObj.getMileage());
                trafficFacilities.setCrossNagm(crossPositionObj.getNagm());
                trafficFacilities.setCrossNagmNumber(crossPositionObj.getNagmNumber());
                trafficFacilities.setCrossNagmMileage(crossPositionObj.getNagmMileage());
            }
        } else {
            // 计算起始绝对里程
            PositionObj beginPositionObj = getPositionInfo(trafficFacilities.getPipeLineId(), trafficFacilities.getBeginX(), trafficFacilities.getBeginY());
            if (beginPositionObj != null) {
                trafficFacilities.setBeginMileage(beginPositionObj.getMileage());
                trafficFacilities.setBeginNagm(beginPositionObj.getNagm());
                trafficFacilities.setBeginNagmNumber(beginPositionObj.getNagmNumber());
                trafficFacilities.setBeginNagmMileage(beginPositionObj.getNagmMileage());
            }
            // 计算终点绝对里程
            PositionObj endPositionObj = getPositionInfo(trafficFacilities.getPipeLineId(), trafficFacilities.getEndX(), trafficFacilities.getEndY());
            if (endPositionObj != null) {
                trafficFacilities.setEndMileage(endPositionObj.getMileage());
                trafficFacilities.setEndNagm(endPositionObj.getNagm());
                trafficFacilities.setEndNagmNumber(endPositionObj.getNagmNumber());
                trafficFacilities.setEndNagmMileage(endPositionObj.getNagmMileage());
            }
        }

        // 添加 图片信息
        String bizzId = StringUtils.isNotNullEmpty(trafficFacilities.getId()) ? trafficFacilities.getId() : CommonUtil.getUUID();
        for (String id : imgs) {
            AttachmentUtil.updateBusinessId(bizzId, id);
        }

        if (StringUtils.isNullEmpty(trafficFacilities.getId())){
            TrafficFacilitiesExample trafficFacilitiesExample = new TrafficFacilitiesExample();
            trafficFacilitiesExample.createCriteria().andDeletedEqualTo(Short.valueOf("0")).andNameEqualTo(trafficFacilities.getName());
            int countNum = trafficFacilitiesMapper.countByExample(trafficFacilitiesExample);
            Asserts.assertLogic(countNum > 0, "名称为:“"+trafficFacilities.getName()+"”交通设施 已经存在，不能重复插入");

            trafficFacilities.setId(bizzId);
            trafficFacilities.setCreateDate(new Date());
            trafficFacilities.setCreateUser(realName);
            trafficFacilities.setDataStated(DataStatedEnum.ALREADY_RELEASED.getCode());
            trafficFacilities.setIsNewest(Short.valueOf(IsNewDateEnum.YES.getCode()));
            return trafficFacilitiesMapper.insertSelective(trafficFacilities);
        }

        TrafficFacilities trafficFacilitiesObj = this.find(trafficFacilities.getId());
        Asserts.assertLogic(trafficFacilitiesObj == null, "交通设施 不存在");
        Asserts.assertLogic(isAuditReleased(trafficFacilitiesObj.getDataStated()), "该 交通设施 已审核通过,不允许编辑");
        if (isAuditDismissed(trafficFacilitiesObj.getDataStated())){
            trafficFacilities.setDataStated(DataStatedEnum.ALREADY_RELEASED.getCode());
        }
        return trafficFacilitiesMapper.updateByPrimaryKeySelective(trafficFacilities);
    }

    @Override
    public int audit(TrafficFacilities trafficFacilities) {
        TrafficFacilities trafficFacilitiesObj = this.find(trafficFacilities.getId());
        Asserts.assertLogic(trafficFacilitiesObj == null, "交通设施 不存在");
        Asserts.assertLogic(isAudit(trafficFacilitiesObj.getDataStated()), "该 交通设施 已审核,不能重复审核");
        return trafficFacilitiesMapper.updateByPrimaryKeySelective(trafficFacilities);
    }

    @Override
    public int delete(String id) {
        TrafficFacilities trafficFacilities = this.find(id);
        Asserts.assertLogic(trafficFacilities == null, "第三方管道及设施 为空");
        Asserts.assertLogic(isAuditReleased(trafficFacilities.getDataStated()), "该 第三方管道及设施 已审核通过,不能“删除”");
        trafficFacilities = new TrafficFacilities();
        trafficFacilities.setId(id);
        trafficFacilities.setDeleted(Short.valueOf("1"));
        trafficFacilities.setUpdateDate(new Date());
        trafficFacilities.setUpdateUser(realName);
        trafficFacilities.setUpdateRemark("删除");
        return trafficFacilitiesMapper.updateByPrimaryKeySelective(trafficFacilities);
    }
}
